/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.desarrollosw2.dao;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import mx.its.desarrollosw2.DetallePrestamo;
import mx.its.desarrollosw2.Libro;
import mx.its.desarrollosw2.Prestamo;
import mx.its.dsw2.biblioteca.enumeracion.EstadoDetallePrestamo;

/**
 *
 * @author sistemas
 */
@Stateless
public class DetallePrestamoDao extends AbstractDao<DetallePrestamo> implements DetallePrestamoDaoLocal{
    @PersistenceContext(unitName = "Biblioteca8EE-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public DetallePrestamoDao() {
        super(DetallePrestamo.class);
    }
    @Override
    public List<DetallePrestamo>findLends(Prestamo lends){
        Query q=em.createNamedQuery("DetallePrestamo.findLends");
        q.setParameter("estado", EstadoDetallePrestamo.PRESTADO.ordinal());
        q.setParameter("idUsuario", lends.getId());
        return q.getResultList();
        
    }

    @Override
    public DetallePrestamo findBooks(Libro book) {
        Query q=em.createNamedQuery("DetallePrestamo.findBooks");
        q.setParameter("estado", EstadoDetallePrestamo.PRESTADO.ordinal());
        q.setParameter("idLibro", book.getId());
        if(q.getResultList().isEmpty()){
            return null;
        }else{
            return(DetallePrestamo)q.getResultList().get(0);
        }
        
    }

   

}
